import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public class Training {
	private final static int WORKDAYS = 5;
	private final static int NUMOFSPLITS = 3;
	
	public List<List> generatePlan(Set<Excercise> metabolicSet,Set<Excercise> gymnasticSet,Set<Excercise> weightliftingSet, int days){
		int times = days/WORKDAYS +days%WORKDAYS;
		List<Excercise> excercises = new ArrayList<Excercise>();
		List<List> excerciseForToday = new ArrayList<List>();
		List<Excercise> metabolic = new ArrayList<Excercise>(metabolicSet);
		List<Excercise> gymnastic = new ArrayList<Excercise>(gymnasticSet);
		List<Excercise> weightlifting = new ArrayList<Excercise>(weightliftingSet);	
		int typeOfexc = 0; //Types: 0-metabolic, 1- gymnastic, 2-weightlifting;
		for(int split=0; split<times ;split++){
			for(int workday=1; workday<WORKDAYS+1 ; workday++){
				if(split%3==0 || weightlifting.size()<3 || gymnastic.size()<3 || metabolic.size()<3){
					metabolic = new ArrayList<Excercise>(metabolicSet);
					gymnastic = new ArrayList<Excercise>(gymnasticSet);
					weightlifting = new ArrayList<Excercise>(weightliftingSet);
					Collections.shuffle(metabolic);
					Collections.shuffle(gymnastic);
					Collections.shuffle(weightlifting);
				}
				if(typeOfexc>2){
					typeOfexc=0;
				}
				for(int excForToday=0; excForToday<workday; excForToday++){
					if(workday==4){
						excForToday+=2;
					}
					if(workday==5){
						excForToday+=4;
					}
					switch(typeOfexc){
					case 0:
						typeOfexc++;
						excercises.add(metabolic.remove(0));
						break;
					case 1:
						typeOfexc++;
						excercises.add(gymnastic.remove(0));
						break;
					case 2:
						excercises.add(weightlifting.remove(0));
						typeOfexc=0;
						break;
					}
				}				
				excerciseForToday.add(excercises);
				excercises = new ArrayList<Excercise>();
				if(split*WORKDAYS+workday==days){
					workday++;
					break;
				}
			}
			typeOfexc++;
		}
		return excerciseForToday;
	}
}